package com.jh.user.pojo.freeQuick;

import cn.jh.common.pay.jdpush.FreeQuickRequestType;
import cn.jh.common.pay.jdpush.OrderStatus;
import cn.jh.common.pay.utils.PayBarConst;
import com.google.common.base.MoreObjects;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;
import java.util.Objects;

/**
 *
 * @author Yichuan
 * @version 1.0
 */
@Entity
@Table(name = "t_pay_bar_request_log")
@EntityListeners(AuditingEntityListener.class)
public class PayBarRequestOrder extends AbstractRecording<Long> {

    private static final long serialVersionUID = 1857541890525203197L;

    public static PayBarRequestOrder createOrder(String merchantId, String outSerialNo,
                                                 FreeQuickRequestType requestType, String merchantNo, String productCode) {
        PayBarRequestOrder order = new PayBarRequestOrder();
        order.setRequestNo(PayBarConst.getOrderIdByUUId(requestType.getCode()));
        order.setOutSerialNo(outSerialNo);
        order.setMerchantId(merchantId);
        order.setRequestType(requestType);
        order.setProductCode(productCode);
        order.setMerchantNo(merchantNo);
        order.setStatus(OrderStatus.PENDING);
        return order;
    }

    /**
     * 请求号
     */
    @Column(name = "request_no")
    private String requestNo;

    /**
     * 商户api请求的流水号
     */
    @Column(name = "out_serial_no")
    private String outSerialNo;

    /**
     * 商户信息id
     */
    @Column(name = "merchant_id")
    private String merchantId;

    /**
     * 返回商户号
     */
    @Column(name = "merchant_no")
    private String merchantNo;

    /**
     * 类型
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "request_type")
    private FreeQuickRequestType requestType;

    /**
     * 产品编码
     */
    @Column(name = "product_code")
    private String productCode;

    /**
     * 状态("PENDING"、"SUCCESS"、"FAILURE")
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "status")
    private OrderStatus status;

    /**
     * 返回信息
     */
    @Column(name = "content")
    private String content;

    /**
     * 备注
     */
    @Column(name = "remark")
    private String remark;

    public PayBarRequestOrder() {
        super();
    }

    public String getRequestNo() {
        return requestNo;
    }

    public void setRequestNo(String requestNo) {
        this.requestNo = requestNo;
    }

    public String getOutSerialNo() {
        return outSerialNo;
    }

    public void setOutSerialNo(String outSerialNo) {
        this.outSerialNo = outSerialNo;
    }

    public String getMerchantId() {
        return merchantId;
    }

    public void setMerchantId(String merchantId) {
        this.merchantId = merchantId;
    }

    public FreeQuickRequestType getRequestType() {
        return requestType;
    }

    public void setRequestType(FreeQuickRequestType requestType) {
        this.requestType = requestType;
    }

    public String getProductCode() {
        return productCode;
    }

    public void setProductCode(String productCode) {
        this.productCode = productCode;
    }

    public OrderStatus getStatus() {
        return status;
    }

    public void setStatus(OrderStatus status) {
        this.status = status;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getMerchantNo() {
        return merchantNo;
    }

    public void setMerchantNo(String merchantNo) {
        this.merchantNo = merchantNo;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof PayBarRequestOrder)) return false;
        if (!super.equals(o)) return false;
        PayBarRequestOrder that = (PayBarRequestOrder) o;
        return Objects.equals(getRequestNo(), that.getRequestNo()) &&
                Objects.equals(getOutSerialNo(), that.getOutSerialNo()) &&
                Objects.equals(getMerchantId(), that.getMerchantId()) &&
                Objects.equals(getMerchantNo(), that.getMerchantNo()) &&
                getRequestType() == that.getRequestType() &&
                Objects.equals(getProductCode(), that.getProductCode()) &&
                Objects.equals(getStatus(), that.getStatus()) &&
                Objects.equals(getContent(), that.getContent()) &&
                Objects.equals(getRemark(), that.getRemark()) &&
                Objects.equals(getCreatedAt(), that.getCreatedAt()) &&
                Objects.equals(getUpdatedAt(), that.getUpdatedAt());
    }

    @Override
    public int hashCode() {
        return Objects.hash(super.hashCode(), getRequestNo(), getOutSerialNo(),
                getMerchantId(), getMerchantNo(), getRequestType(), getProductCode(), getStatus(),
                getContent(), getRemark(), getCreatedAt(), getUpdatedAt());
    }

    @Override
    public String toString() {
        return MoreObjects.toStringHelper(this)
                .add("requestNo", requestNo)
                .add("outSerialNo", outSerialNo)
                .add("merchantId", merchantId)
                .add("merchantNo", merchantNo)
                .add("requestType", requestType)
                .add("productCode", productCode)
                .add("status", status)
                .add("content", content)
                .add("remark", remark)
                .add("createAt", getCreatedAt())
                .add("updateAt", getUpdatedAt())
                .toString();
    }
}
